package cn.gjxblog.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import sun.rmi.server.LoaderHandler;

/**
 * 作用: 第二种连接服务接口方法
 * 作者:gjx
 * 版本:V1.0
 * 创建时间: 2018/7/27 18:39
 */

@RestController
@RequestMapping("/test2")
public class TestController2 {

    @Autowired
    private LoadBalancerClient loadBalancerClient;


    @RequestMapping("/test")
    public String test(String name){

        ServiceInstance serviceInstance = loadBalancerClient.choose("HELLO-SERVICE");
        String host = serviceInstance.getHost();
        int port = serviceInstance.getPort();

        RestTemplate restTemplate = new RestTemplate();
        String forObject = restTemplate.getForObject("http://"+host+":"+port+"/hello/hello?name=" + name, String.class);

        return forObject;
    }


}
